package com.eway.viewanimator;

import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class LayoutTranslationActivity extends AppCompatActivity implements View.OnClickListener {

	private Button layoutTranslationAdd, layoutTranslationDel;
	private LinearLayout layoutTranslationContain;
	private int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_layout_translation);


		layoutTranslationAdd = (Button) findViewById(R.id.layout_translation_add);
		layoutTranslationDel = (Button) findViewById(R.id.layout_translation_del);
		layoutTranslationContain = (LinearLayout) findViewById(R.id.layout_translation_contain);

		layoutTranslationAdd.setOnClickListener(this);
		layoutTranslationDel.setOnClickListener(this);


		addLayoutTranslation();
		delLayoutTranslation();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

			case R.id.layout_translation_add:
				i++;
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				Button button = new Button(LayoutTranslationActivity.this);
				button.setText("btn" + i);
				button.setLayoutParams(layoutParams);
				layoutTranslationContain.addView(button);
				break;
			case R.id.layout_translation_del:
				if (i > 0) {
					layoutTranslationContain.removeViewAt(0);
				}
				break;
		}
	}

	private void addLayoutTranslation() {
		LayoutTransition layoutTransition = new LayoutTransition();
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(null, "rotationY", 0f, 360f, 0);
		objectAnimator.setDuration(2000);
		layoutTransition.setAnimator(LayoutTransition.APPEARING, objectAnimator);
		layoutTranslationContain.setLayoutTransition(layoutTransition);

	}

	private void delLayoutTranslation() {
		LayoutTransition layoutTransition = new LayoutTransition();
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0);
		layoutTransition.setAnimator(LayoutTransition.DISAPPEARING, objectAnimator);
		layoutTranslationContain.setLayoutTransition(layoutTransition);

	}
}
